﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HMIS.MVC.ViewModel;
using HMIS.MVC.CommonSupport;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.FrameWork.Cache;
using System.ComponentModel;

namespace HMIS.MVC.Controllers
{
    public class InfraActionPermissionController : BaseController
    {
        private readonly IActionPermissionFacade ActionPermissionFacade;
        private readonly IAuthorityFacade AuthorityFacade;
        private readonly ICache Cache;
        public InfraActionPermissionController(IActionPermissionFacade ActionPermissionFacade, IAuthorityFacade AuthorityFacade, ICache Cache)
        {
            this.ActionPermissionFacade = ActionPermissionFacade;
            this.AuthorityFacade = AuthorityFacade;
            this.Cache = Cache;
        }

        [Description("访问功能权限管理页面")]
        [ViewPage]
        public ActionResult ActionPermission()
        {
            return View();
        }

        #region ActionPermission

        [Description("获取功能权限列表")]
        public ActionResult GetPlistActionPermission()
        {
            long total;
            var data = ActionPermissionFacade.GetPlistAll(
                int.Parse(Request["start"]),
                int.Parse(Request["limit"]),
                Request["sort"],
                Request["dir"],
                this.DesDataFilter(Request["filter"]),
                out total);
            return this.JsonFormat(
                new ExtGirdData<ActionPermission> { total = total, data = data });
        }

        [Description("获取功能权限下拉列表")]
        public ActionResult QueryActionPlist()
        {
            long total;
            var data = ActionPermissionFacade.QueryActionPlist(
                Request["query"],
                int.Parse(Request["start"]),
                int.Parse(Request["limit"]),
                out total);
            return this.JsonFormat(
                new ExtGirdData<ActionPermission> { total = total, data = data });
        }

        [ExtResult]
        [Description("添加、修改功能权限")]
        public ActionResult SaveOrUpdateActionPermission()
        {
            ActionPermission entity;
            if (String.IsNullOrEmpty(Request["Id"]))
            {
                entity = new ActionPermission
                {
                    ActionName = Request["ActionName"],
                    ControllerName = Request["ControllerName"],
                    Description = Request["Description"]
                };
            }
            else
            {
                entity = ActionPermissionFacade.GetActionPermission(Request["Id"]);
                entity.Description = Request["Description"];
            }

            ActionPermissionFacade.SaveOrUpdate(entity);
            return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
        }

        //判断字段唯一
        [ExtResult]
        [Description("判断功能权限Controller名称和Action名称的组合全局是否唯一")]
        public ActionResult IsFieldExitsForActionPermission()
        {
            var result = ActionPermissionFacade.IsFieldExistForActionPermission(Request["FieldName"], Request[Request["FieldName"]], Request["Id"], Request["ControllerName"]);
            return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
        }

        [ExtResult]
        [Description("删除功能权限")]
        public ActionResult DeleteActionPermission()
        {
            ActionPermissionFacade.DeleteActionPermission(Request["Id"]);
            return this.JsonFormat((new ExtResult { success = true }));
        }

        //重置整个系统的功能权限缓存
        [ExtResult]
        [Description("重置整个系统的功能权限缓存")]
        public ActionResult ResetCache()
        {
            Cache.RemoveApplicationCache("actionpermission");
            AuthorityFacade.GetAllActionPermission();
            return this.JsonFormat(new ExtResult { success = true });
        }
        #endregion
    }
}
